﻿using System;

namespace CodeLibrary.Extensions
{
    public static class MathematicalExtensions
    {
        public static bool IsPrime(this int val)
        {
            if ((val & 1) == 0)
            {
                return val == 2;
            }
            int num = (int)Math.Sqrt((double)val);
            for (int i = 3; i <= num; i += 2)
            {
                if ((val % i) == 0)                
                    return false;                
            }
            return true;
        }
        
        public static double Factorial(this int val)
        {
            if (val <= 1)
                return 1;
            return val * Factorial(val - 1);
        }

        public static double PercentOf(this double val,double value,int roundOffTo)
        {
            return Math.Round((val / 100d) * value, roundOffTo);
        }
        
        public static double PercentOf(this int val, double value, int roundOffTo)
        {
            return Math.Round((val / 100d) * value, roundOffTo);
        }
        
        public static double PowerOf(this int val,int off)
        {
            return Math.Pow(val, off);
        }
    }
}
